home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / Word8Array.mlp < prev    next >
Encoding:
Text File  |  1997-08-18  |  4.6 KB  |  150 lines  |  [TEXT/R*ch]

  1. (* Word8Array -- as of 1994-12-21 *)
  2.  
  3. type elem   = Word8.word;
  4. type vector = Word8Vector.vector;
  5.  
  6. local 
  7.     prim_eqtype array_;
  8.     prim_val array_   : int -> array_                 = 1 "create_string";
  9.     prim_val vector_  : int -> vector                 = 1 "create_string";
  10.     prim_val sub_     : array_ -> int -> elem         = 2 "get_nth_char";
  11.     prim_val update_  : array_ -> int -> elem -> unit = 3 "set_nth_char";
  12.     prim_val length_  : array_ -> int                 = 1 "string_length";
  13.     prim_val lengthv_ : vector -> int                 = 1 "string_length";
  14.     prim_val fill_    : array_ -> int -> int -> elem -> unit 
  15.                                                       = 4 "fill_string";
  16.     prim_val blitaa_  : array_ -> int -> array_ -> int -> int -> unit 
  17.                                                       = 5 "blit_string";
  18.     prim_val blitav_  : array_ -> int -> vector -> int -> int -> unit 
  19.                                                       = 5 "blit_string";
  20.     prim_val blitva_  : vector -> int -> array_ -> int -> int -> unit 
  21.                                                       = 5 "blit_string";
  22. in 
  23.  
  24. type array = array_ ref;
  25.  
  26. #include "../config/m.h"
  27. #ifdef SIXTYFOUR
  28. val maxLen = 144115188075855863;      (* = (2^54-1)*8-1, with 64 bit *)
  29. #else
  30. val maxLen = 16777211;          (* = (2^22-1)*4-1, with 32 bit *)
  31. #endif
  32.  
  33. val array0 = ref (array_ 0);
  34.  
  35. fun array(n, v: elem) =
  36.     let val a = if n < 0 orelse n > maxLen then raise Size else array_ n 
  37.     in fill_ a 0 n v; ref a end;
  38.  
  39. fun tabulate(n, f : int -> elem) =
  40.   if n < 0 orelse n > maxLen then raise Size else
  41.   let val a = array_ n
  42.       fun init i = if i >= n then () else (update_ a i (f i); init (i+1))
  43.   in init 0; ref a end;
  44.  
  45. fun fromList (vs : elem list) =
  46.     let val n = List.length vs
  47.     val a = if n > maxLen then raise Size else array_ n 
  48.     fun init [] i = ()
  49.       | init (v::vs) i = (update_ a i v; init vs (i+1))
  50.     in init vs 0; ref a end;
  51.  
  52. fun length (ref a) = length_ a;
  53.  
  54. fun sub(ref a, i) =
  55.   if i < 0 orelse i >= length_ a then raise Subscript 
  56.   else sub_ a i;
  57.  
  58. fun update(ref a, i, v) =
  59.   if i < 0 orelse i >= length_ a then raise Subscript 
  60.   else update_ a i v;
  61.  
  62. fun extract (ref a, i, slicelen) =
  63.     let val n = case slicelen of NONE => length_ a - i | SOME n => n 
  64.     val newvec = if i<0 orelse n<0 orelse i+n > length_ a then
  65.                          raise Subscript
  66.              else
  67.                  vector_ n 
  68.     in blitav_ a i newvec 0 n; newvec end;
  69.  
  70. fun copy {src = ref a1: array, si=i1, len, dst = ref a2: array, di=i2} =
  71.     let val n = case len of NONE => length_ a1 - i1 | SOME k => k
  72.     in
  73.     if n<0 orelse i1<0 orelse i1+n > length_ a1
  74.            orelse i2<0 orelse i2+n > length_ a2
  75.     then raise Subscript
  76.     else blitaa_ a1 i1 a2 i2 n
  77.     end
  78.  
  79. fun copyVec {src = a1: vector, si=i1, len, dst = ref a2: array, di=i2} =
  80.     let val n = case len of NONE => lengthv_ a1 - i1 | SOME k => k
  81.     in
  82.     if n<0 orelse i1<0 orelse i1+n > lengthv_ a1
  83.                orelse i2<0 orelse i2+n > length_ a2
  84.         then raise Subscript
  85.     else blitva_ a1 i1 a2 i2 n
  86.     end
  87.  
  88. fun foldl f e (ref a) = 
  89.     let val stop = length_ a
  90.     fun lr j res = if j < stop then lr (j+1) (f(sub_ a j, res))
  91.                else res
  92.     in lr 0 e end
  93.  
  94. fun foldr f e (ref a) =
  95.     let fun rl j res = if j >= 0 then rl (j-1) (f(sub_ a j, res))
  96.                else res
  97.     in rl (length_ a - 1) e end
  98.  
  99. fun modify f (ref a) = 
  100.     let val stop = length_ a
  101.     fun lr j = if j < stop then (update_ a j (f(sub_ a j)); lr (j+1))
  102.            else ()
  103.     in lr 0 end
  104.  
  105. fun app f (ref a) = 
  106.     let val stop = length_ a
  107.     fun lr j = if j < stop then (f(sub_ a j); lr (j+1))
  108.            else ()
  109.     in lr 0 end
  110.  
  111. fun sliceend (a, i, NONE) = 
  112.         if i<0 orelse i>length a then raise Subscript
  113.     else length a
  114.   | sliceend (a, i, SOME n) = 
  115.     if i<0 orelse n<0 orelse i+n>length a then raise Subscript
  116.     else i+n;
  117.  
  118. fun foldli f e (slice as (ref a, i, _)) = 
  119.     let fun loop stop =
  120.         let fun lr j res = 
  121.         if j < stop then lr (j+1) (f(j, sub_ a j, res))
  122.         else res
  123.         in lr i e end
  124.     in loop (sliceend slice) end;
  125.  
  126. fun foldri f e (slice as (ref a, i, _)) = 
  127.     let fun loop start =
  128.         let fun rl j res = 
  129.             if j >= i then rl (j-1) (f(j, sub_ a j, res))
  130.             else res
  131.         in rl start e end;
  132.     in loop (sliceend slice - 1) end
  133.  
  134. fun modifyi f (slice as (ref a, i, _)) = 
  135.     let fun loop stop =
  136.         let fun lr j = 
  137.         if j < stop then (update_ a j (f(j, sub_ a j)); lr (j+1))
  138.         else ()
  139.         in lr i end
  140.     in loop (sliceend slice) end;
  141.  
  142. fun appi f (slice as (ref a, i, _)) = 
  143.     let fun loop stop = 
  144.         let    fun lr j = 
  145.             if j < stop then (f(j, sub_ a j); lr (j+1)) 
  146.             else ()
  147.         in lr i end
  148.     in loop (sliceend slice) end;
  149. end
  150.